package com.neusoft.databus.sap.ssh;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.neusoft.bizcore.web.utils.JsonUtils;
import com.neusoft.databus.agent.bean.PoolStatusBean;
import com.neusoft.databus.agent.pool.serial.SerialConnector;
import com.neusoft.databus.agent.pool.snmp.SnmpConnector;
import com.neusoft.databus.agent.pool.socket.SocketConnector;
import com.neusoft.databus.agent.pool.socket2.Socket2Connector;
import com.neusoft.databus.agent.service.SupportService;
import com.neusoft.databus.common.connection.SerialConnection;
import com.neusoft.databus.common.connection.SnmpConnection;
import com.neusoft.databus.common.connection.SocketConnection;

import sshd.shell.springboot.autoconfiguration.SshdShellCommand;

@Component
@SshdShellCommand(value = "connpool", description = "connection pool. Type 'connpool' for supported subcommands")
public class ConnPoolCommand {

    @Autowired
    private SupportService supportService;
    @Autowired
    private SocketConnector socketConnector;
    @Autowired
    private Socket2Connector socket2Connector;
    @Autowired
    private SnmpConnector snmpConnector;
    @Autowired
    private SerialConnector serialConnector;

    @SshdShellCommand(value = "list", description = "list all . Usage: connpool list")
    public String list(final String arg) throws IOException {
        final List<PoolStatusBean> beans = this.supportService.realtimePoolsStatus();
        return JsonUtils.pojoToPrettyJson(beans);
    }

    @SuppressWarnings("deprecation")
    @SshdShellCommand(value = "clear", description = "clear by key . Usage: connpool clear socket 192.168.1.103:8080 \n connpool clear serial COM1")
    public String clear(final String arg) throws IOException {
        if (StringUtils.isEmpty(arg)) {
            return "illegal command";
        }
        final String[] arguments = arg.split("\\s+");
        if (arguments.length != 2) {
            return "illegal command";
        }
        switch (arguments[0]) {
        case "socket":
        case "socket2":
        case "snmp":
            final String[] tuple = arguments[1].split(":");
            if ("socket".equalsIgnoreCase(arguments[0])) {
                final SocketConnection connection = new SocketConnection();
                connection.setIpAddress(tuple[0]);
                connection.setPort(Integer.valueOf(tuple[1]));
                this.socketConnector.removeAllConns(connection);
            } else if ("socket2".equalsIgnoreCase(arguments[0])) {
                final SocketConnection connection = new SocketConnection();
                connection.setIpAddress(tuple[0]);
                connection.setPort(Integer.valueOf(tuple[1]));
                this.socket2Connector.removeAllConns(connection);
            } else if ("snmp".equalsIgnoreCase(arguments[0])) {
                final SnmpConnection connection = new SnmpConnection();
                connection.setIpAddress(tuple[0]);
                connection.setPort(Integer.valueOf(tuple[1]));
                connection.setCommunity(tuple[2]);
                this.snmpConnector.removeAllConns(connection);
            }
            break;

        case "serial":
            final SerialConnection connection = new SerialConnection();
            connection.setComValue(arguments[1]);
            this.serialConnector.removeAllConns(connection);
            break;
        default:
            break;
        }

        return arg + " is cleared";
    }

}
